//
//  ProjectChangeHeaderView.m
//  Estate
//
//  Created by heyong on 2016/11/13.
//  Copyright © 2016年 ttsx. All rights reserved.
//

#import "ProjectChangeHeaderView.h"

@implementation ProjectChangeHeaderView

-(UILabel *)lab_1{
    if(_lab_1==nil){
        _lab_1=[[UILabel alloc]init];
    }
    return _lab_1;
}

-(UILabel *)lab_2{
    if(_lab_2==nil){
        _lab_2=[[UILabel alloc]init];
    }
    return _lab_2;
}

-(void)setModel:(personModel *)model{
    _model=model;
    /*
     *  甲方  巡检员  巡检组长 显示智慧监理
     */
    NSString *role=DEF_PERSISTENT_GET_OBJECT(@"role");
    if([USERTYPE.typeArray containsObject:role]||[USERTYPE.typeArrayNoWeather containsObject:role]){
        if([role isEqualToString:@"BUILD_01"]||[role isEqualToString:@"BUILD_02"]){
            self.lab_1.text=_model.projectName;
        }else{
            self.lab_1.text=@"智慧监理";
        }
    }else{
        if(!_model.projectName){
            self.lab_1.text=@"智慧监理";
        }else{
            self.lab_1.text=_model.projectName;
            
        }
    }
    
    if([role isEqualToString:@"USER_01"]){
        _model.position=@"总监";
    }
    else if([role isEqualToString:@"USER_03"]){
        _model.position=@"监理员";
    }
    else if([role isEqualToString:@"USER_07"]){
        _model.position=@"专业监理工程师";
    }
    else if([role isEqualToString:@"USER_04"]){
        _model.position=@"监理安全工程师";
    }
    else if([role isEqualToString:@"CONSTRACT_01"]){
        _model.position=@"施工单位项目经理";
    }
    else if([role isEqualToString:@"CONSTRACT_02"]){
        _model.position=@"施工单位总工";
    }
    else if([role isEqualToString:@"CONSTRACT_03"]){
        _model.position=@"施工单位安全总监";
    }
    else if([role isEqualToString:@"CONSTRACT_04"]){
        _model.position=@"施工单位质检员";
    }
    else if([role isEqualToString:@"CONSTRACT_05"]){
        _model.position=@"施工单位安全员";
    }
    else if([role isEqualToString:@"CONSTRACT_06"]){
        _model.position=@"施工单位实验员";
    }
    else if([role isEqualToString:@"CONSTRACT_07"]){
        _model.position=@"施工单位工长";
    }else if([role isEqualToString:@"1"]){
        _model.position=@"巡检组长";
    }else if([role isEqualToString:@"2"]){
        _model.position=@"巡检员";
    }else if([role isEqualToString:@"LEADER_01"]){
        _model.position=@"领导";
    }else if([role isEqualToString:@"BUILD_01"]){
        _model.position=@"建设单位领导";
    }else if ([role isEqualToString:@"BUILD_02"]){
        _model.position=@"建设单位工程部";
    } else if ([role isEqualToString:@"CONSTRACT_08"]) {
        _model.position = @"检查单位实验员";
    }
    
    self.lab_2.text=[NSString stringWithFormat:@"%@|%@",_model.position,_model.name];
}

- (instancetype)initWithFrame:(CGRect)frame{
    if(self=[super initWithFrame:frame]){
        [self createView];
    }
    return self;
}

//创建视图
- (void)createView{
    WEAKSELF
    headImage=[[UIView alloc]initWithFrame:CGRECTS(0, 0, 375, DEF_HEIGHT(self))];
    //    headImage.image=[UIImage imageNamed:@""];
    headImage.backgroundColor=[UIColor whiteColor];
    [self addSubview:headImage];
    
    //添加头像视图
    txImg=[[UIImageView alloc]init];
    txImg.backgroundColor=[UIColor whiteColor];
    [headImage addSubview:txImg];
    [txImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.mas_centerY).offset(10*BIZ);
        make.left.offset(20*BIZ);
        make.width.offset(70*BIZ);
        make.height.offset(70*BIZ);
    }];
    txImg.layer.cornerRadius=35.*BIZ;
    txImg.layer.masksToBounds=YES;
    
    //存在头像连接
    if(DEF_PERSISTENT_GET_OBJECT(@"headImage")){
        NSString *urlStr=[NSString stringWithFormat:@"%@/%@",API_HOST,DEF_PERSISTENT_GET_OBJECT(@"headImage")];
        [txImg sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:[UIImage imageNamed:@"touxiang_de"] options:SDWebImageRefreshCached];
    }
    //发送请求
    else{
        NSMutableDictionary *parame=[NSMutableDictionary dictionary];
        [parame setValue:USERID forKey:@"userId"];
        [LSHttpTool Post:URL_HEADPHOTO_SELECT parameters:parame success:^(id responseObject){
            NSLog(@"个人信息：%@/nmsg:%@",responseObject,responseObject[@"msg"]);
            if([responseObject[@"code"]isEqualToString:@"1"]){
                NSDictionary *dic=responseObject[@"data"];
                //记录头像URL
                DEF_PERSISTENT_SET_OBJECT(dic[@"headUrl"], @"headImage");
                NSString *urlStr=dic[@"headUrl"];
                [txImg sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:[UIImage imageNamed:@"touxiang_de"] options:SDWebImageRefreshCached];
            }
        } failure:^(NSError *error) {
            NSLog(@"error:%@",error);
            txImg.image=[UIImage imageNamed:@"touxiang_de"];
        }];
    }
    //添加项目名
    self.lab_1.textColor=UIColorFromRGB(0x787878);
    self.lab_1.font=[UIFont systemFontOfSize:16.*BIZ];
    [headImage addSubview:self.lab_1];
    [self.lab_1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(txImg.mas_right).offset(10*BIZ);
        make.top.equalTo(txImg.mas_top).offset(18*BIZ);
        make.right.offset(-20*BIZ);
        make.height.offset(15*BIZ);
    }];
    //添加名称
    self.lab_2.textColor=UIColorFromRGB(0x787878);
    self.lab_2.font=[UIFont systemFontOfSize:16.*BIZ];
    [headImage addSubview:self.lab_2];
    [self.lab_2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(txImg.mas_right).offset(30*BIZ);
        make.bottom.equalTo(txImg.mas_bottom).offset(-18*BIZ);
        make.right.mas_offset(-10);
        make.height.offset(10*BIZ);
    }];
    
    UIImageView *img_1=[[UIImageView alloc]init];
    img_1.image=[UIImage imageNamed:@"renwu_h"];
    [headImage addSubview:img_1];
    [img_1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(txImg.mas_right).offset(10*BIZ);
        make.centerY.mas_equalTo(weakSelf.lab_2.mas_centerY);
        make.width.offset(15*BIZ);
        make.height.offset(15*BIZ);
    }];
}

@end
